package com.app.xinfadi.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.app.xinfadi.base.MyApplication;


/**
 * @author WuTengFei
 * @Function:配置文件工具类 <br/>
 * @date 2014-11-30 下午1:58:46 <br/>
 */
public class SharedPreferencesUtil {
    public final String TAG = "SharedPreferencesUtil";
    public static SharedPreferencesUtil sInstance;
    private SharedPreferences sp;
    private Editor editor;
    private Context mContext;
    private static final String USER_PHONE = "user_phone";
    private static final String USER_NICK_NAME = "user_nick_name";
    private static final String USER_REAL_NAME = "user_real_name";
    private static final String USER_PASSWORD = "user_password";
    private static final String USER_STATE = "user_state";
    private static final String USER_ID = "user_id";
    private static final String USER_SEX = "user_sex";
    private static final String USER_IMAGE_URL = "user_image_url";
    private static final String USER_LOGIN_NAME = "user_login_name";
    private static final String USER_ROLE = "user_role";

    private static final String JPUSH_STATE = "is_open_jpush";

    /**
     * 私有构造 , 不允许实例化
     */
    private SharedPreferencesUtil() {
        mContext = MyApplication.getInstance().getApplicationContext();
        sp = mContext.getSharedPreferences(TAG, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    /**
     * 单例模式
     *
     * @return
     */
    public synchronized static SharedPreferencesUtil getInstance() {
        if (sInstance == null) {
            sInstance = new SharedPreferencesUtil();
        }
        return sInstance;
    }

    /**
     * 查询是否是第一次安装
     *
     * @return
     */
    public boolean isFirstInstall() {
        return sp.getBoolean("FIRST_INSTALL", true);
    }

    /**
     * 保存第一次安装
     */
    public void saveFirstInstall() {
        editor.putBoolean("FIRST_INSTALL", false).commit();
    }

    /**
     * 是否显示导航过
     *
     * @return
     */
    public boolean isFirstNavigate() {
        return sp.getBoolean("FIRST_NAVIGATE", true);
    }

    /**
     * 保存已经导航
     */
    public void saveFirstNavigate() {
        editor.putBoolean("FIRST_NAVIGATE", false).commit();
    }

    /**
     * 第一次进入 生成user记录
     */
    public void saveFirstUserInfo() {
        editor.putString(USER_NICK_NAME, "");
        editor.putString(USER_PHONE, "");
        editor.putString(USER_PASSWORD, "");
        editor.putInt(USER_STATE, 0);
        editor.commit();
    }
    public void saveUserInfo(String user_login_name) {
        editor.putString(USER_LOGIN_NAME, user_login_name);
        editor.commit();
    }
    public void saveUserInfo(String user_id,String user_login_name,String user_role,String phone) {
        editor.putString(USER_ID, user_id);
        editor.putString(USER_LOGIN_NAME, user_login_name);
        editor.putString(USER_ROLE, user_role);
        editor.putString(USER_PHONE, phone);

        editor.commit();
    }
    /**
     * 保存用户信息
     *
     * @param user_nick_name
     * @param user_phone
     * @param password
     * @param state
     */
    public void saveUserInfo(String user_nick_name, String user_phone, String password, int state) {
        editor.putString(USER_NICK_NAME, user_nick_name);
        editor.putString(USER_PHONE, user_phone);
        editor.putString(USER_PASSWORD, password);
        editor.putInt(USER_STATE, state);
        editor.commit();
    }

    /**
     * 保存用户信息
     *
     * @param user_id
     * @param user_nick_name
     * @param user_phone
     * @param password
     * @param state
     */
    // public void saveUserInfo(String user_id, String user_nick_name, String
    // user_phone, String password, int state) {
    // editor.putString(USER_ID, user_id);
    // editor.putString(USER_NICK_NAME, user_nick_name);
    // editor.putString(USER_PHONE, user_phone);
    // editor.putString(USER_PASSWORD, password);
    // editor.putInt(USER_STATE, state);
    // editor.commit();
    // }

    /**
     * 保存用户信息
     *
     * @param user_id
     * @param user_nick_name
     * @param user_sex
     * @param user_phone
     * @param password
     * @param state
     */
    public void saveUserInfo(String user_id, String user_nick_name, String user_real_name, String user_sex, String user_phone,
                             String password, String user_image_url, int state) {
        editor.putString(USER_ID, user_id);
        editor.putString(USER_NICK_NAME, user_nick_name);
        editor.putString(USER_REAL_NAME, user_real_name);
        editor.putString(USER_SEX, user_sex);
        editor.putString(USER_PHONE, user_phone);
        editor.putString(USER_PASSWORD, password);
        editor.putInt(USER_STATE, state);
        editor.putString(USER_IMAGE_URL, user_image_url);
        editor.commit();
    }

    /**
     * 保存user_id
     *
     * @param user_id
     */
    public void saveUser_Id(String user_id) {
        editor.putString(USER_ID, user_id).commit();
    }

    /*
     * 获取UserID
     */
    public String getUser_Id() {
        return sp.getString(USER_ID, "");
    }

    public void saveUserSex(String user_sex) {
        editor.putString(USER_SEX, user_sex).commit();
    }

    public String getUseSex() {
        return sp.getString(USER_SEX, "");
    }

    /**
     * 保存用户手机
     *
     * @param user_phone
     */
    public void saveUserPhone(String user_phone) {
        editor.putString(USER_PHONE, user_phone).commit();
    }

    /**
     * 获取用户手机
     *
     * @return
     */
    public String getUserPhone() {
        return sp.getString(USER_PHONE, "");

    }
    public String getUserLoginName() {
        return sp.getString(USER_LOGIN_NAME, "");
    }
    public String getUserRole() {
        return sp.getString(USER_ROLE, "");
    }

    /**
     * 保存用户昵称
     *
     * @param nick_name
     */
    public void saveUserNickName(String nick_name) {
        editor.putString(USER_NICK_NAME, nick_name).commit();
    }

    /**
     * 获取用户昵称
     *
     * @return
     */
    public String getUserNickName() {
        return sp.getString(USER_NICK_NAME, "");
    }

    /**
     * 保存真是姓名
     *
     * @param real_name
     */
    public void saveUserRealName(String real_name) {
        editor.putString(USER_REAL_NAME, real_name).commit();
    }

    /**
     * 获取真实姓名
     *
     * @return
     */
    public String getUserRealName() {
        return sp.getString(USER_REAL_NAME, "");
    }

    /**
     * 保存用户状态 0 未登录 1 登陆
     *
     * @param state
     */
    public void saveUserState(int state) {
        editor.putInt(USER_STATE, state).commit();
    }

    /**
     * 获取 用户状态
     *
     * @return
     */
    public int getUserState() {
        return sp.getInt(USER_STATE, 0);
    }

    /**
     * 保存用户密码
     *
     * @param password
     */
    public void saveUserPassword(String password) {
        editor.putString(USER_PASSWORD, password).commit();
    }

    /**
     * 获取用户密码
     *
     * @return
     */
    public String getUserPassword() {
        return sp.getString(USER_PASSWORD, "");
    }

    /**
     * 保存用户头像
     *
     * @param imageUrl
     */
    public void saveUserImageUrl(String imageUrl) {
        LogUtil.e(TAG,"保存头像url");
        editor.putString(USER_IMAGE_URL, imageUrl).commit();
    }

    /**
     * 设置用户头像
     *
     * @return
     */
    public String getUserImageUrl() {
        return sp.getString(USER_IMAGE_URL, "");
    }


    /**
     * 保存上次总计发送的流量
     *
     * @param lastTotalSend
     */
    public void saveLastTotalSendTraffic(long lastTotalSend) {
        editor.putLong("last_total_send", lastTotalSend).commit();
    }

    /**
     * 查询上次发送的总计流量
     *
     * @return
     */
    public long getLastTotalSendTraffic() {
        return sp.getLong("last_total_send", 0);
    }

    /**
     * 保存上次总计接受的流量
     *
     * @param lastTotalReceive
     */
    public void saveLastTotalReceiveTraffic(long lastTotalReceive) {
        editor.putLong("last_total_receive", lastTotalReceive).commit();
    }

    /**
     * 查询上次总计接受的流量
     *
     * @return
     */
    public long getLastTotalReceiveTraffic() {
        return sp.getLong("last_total_receive", 0);
    }

    /**
     * 保存手机号码
     */
    public void saveSettingPhone(String phone) {
        editor.putString("setting_phone", phone).commit();
    }
}
